﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace WordMemory
{

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public int num;
        public int index = 1;
        public string word;
        public string meaning;
        public int correct = 0;


        private void next_vocabulary(object sender, EventArgs e)
        {
            if (index >= num + 1)
            {
                MessageBox.Show("本次背单词任务完成！你一共写出" + correct + "个单词哦~");
                button2.Enabled = false;
                textBox1.Enabled = false;
            }
            else
            {
                //数据库的连接
                string connString = "Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=D:\\official\\编程\\C#\\WordMemory\\Database1.mdf;Integrated Security=True";
                SqlConnection connection = new SqlConnection(connString);
                connection.ConnectionString = connString;
                connection.Open();
                //数据库查询
                string str = "SELECT * FROM WordMemory WHERE Id=' " + index + "';";
                SqlCommand command = new SqlCommand(str, connection);
                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    meaning = reader["Chinese"].ToString();
                    word = reader["English"].ToString();
                }
                label3.Text = meaning;
                index++;
                button2.Text = "忘了";
                connection.Close();
            }

        }

        //开始按钮，获取所背单词的数目
        private void button1_Click(object sender, EventArgs e)
        {
            button2.Enabled = true;
            label3.Text = "";
            num = (int)numericUpDown1.Value;
            this.next_vocabulary(sender, e);
        }

        //通过回车来判断所填写单词是否正确
        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                if (textBox1.Text == word)
                {
                    correct++;
                    MessageBox.Show("正确！");
                    this.next_vocabulary(sender, e);
                }
                else
                {
                    MessageBox.Show("输得不对！！！");
                }
            }
        }

        //点击忘了按钮会将正确答案填入文本框，并改为下一个按钮
        private void button2_Click(object sender, EventArgs e)
        {
            if (button2.Text == "忘了")
            {
                textBox1.Text = word;
                textBox1.Enabled = false;
                button2.Text = "下一个";
            }
            else
            {
                textBox1.Enabled = true;
                this.next_vocabulary(sender, e);
            }
        }
    }
}
